package com.yujistu.fragment.appointfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.yujistu.R;
import com.yujistu.activity.SearchingActivity;
import com.yujistu.fragment.BaseItemFragment;
import com.yujistu.fragment.ChangesFragment;
import com.yujistu.fragment.PersonalFragment;
import com.yujistu.fragment.PurposeFragment;
import com.yujistu.manager.Observer;
import com.yujistu.manager.ObserverManger;
import com.yujistu.widget.FixedSpeedScroller;

import java.lang.reflect.Field;

/**
 * Created by S3YOGA on 2015/11/4.
 */
public class AppointFragment extends BaseItemFragment implements RippleView.OnRippleCompleteListener {

    RippleView  cityLeftRV;
    TextView    cityText;
    RippleView  fileRightRV;
    RelativeLayout  searchingRL;

    SmartTabLayout  tabLayout;
    ViewPager   viewPager;

    View mContentView;

    public AppointFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mContentView = inflater.inflate(R.layout.fragment_appoint, container, false);

        initView();
        initTabLayout();
        return mContentView;
    }

    private void initTabLayout() {
        ObserverManger.getInstance(ObserverManger.LOCATION_CITY).registerObserver(observer);

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getActivity().getSupportFragmentManager(), FragmentPagerItems.with(getActivity())
                .add("团课", GroupClassFragment.class)
                .add("私教", PersonalFragment.class)
                .add("场地", ChangesFragment.class)
                .add("目的", PurposeFragment.class)
                .add("企业课", EnterpriseFragment.class)
                .create());

        viewPager = (ViewPager)  mContentView.findViewById(R.id.appoint_viewPager);
        viewPager.setOffscreenPageLimit(5);
        try{
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            FixedSpeedScroller scroller = new FixedSpeedScroller(viewPager.getContext(), new OvershootInterpolator());
            field.set(viewPager, scroller);
            scroller.setmDuration(400);
        }catch (Exception e){
            e.printStackTrace();
        }

        viewPager = (ViewPager) mContentView.findViewById(R.id.appoint_viewPager);
        viewPager.setAdapter(adapter);
        tabLayout=(SmartTabLayout) mContentView.findViewById(R.id.tab_layout);
        tabLayout.setViewPager(viewPager);
    }

    private void initView() {
        cityLeftRV = (RippleView) mContentView.findViewById(R.id.city_appointRV);
        cityLeftRV.setOnRippleCompleteListener(this);
        cityText = (TextView) mContentView.findViewById(R.id.locateTV);
        cityText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        fileRightRV = (RippleView) mContentView.findViewById(R.id.filtrate_appointRV);
        fileRightRV.setOnRippleCompleteListener(this);

        searchingRL = (RelativeLayout) mContentView.findViewById(R.id.searchingRL);
        searchingRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchingActivity.class));
            }
        });
    }

    @Override
    public void onComplete(RippleView rippleView) {



    }

    Observer observer = new Observer() {
        @Override
        public void update(Object obj) {
            if (TextUtils.isEmpty(obj.toString())) {
                cityText.setText("定位中");
            } else {
                cityText.setText(obj.toString());
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        ObserverManger.getInstance(ObserverManger.LOCATION_CITY).removeObserver(observer);
    }
}
